<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="表单名称" prop="formName">
        <el-input
          v-model="queryParams.formName"
          placeholder="请输入表单名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['flowable:form:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['flowable:form:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['flowable:form:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['flowable:form:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="formList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="表单主键" align="center" prop="formId" />
      <el-table-column label="表单名称" align="center" prop="formName" />
      <el-table-column label="表单类型" align="center" prop="formType">
        <template slot-scope="scope">
          <div v-if="scope.row.formType">自定义开发</div>
          <div v-else>在线定制</div>
        </template>
      </el-table-column>
      <el-table-column label="路由地址" align="center" prop="formRouterPath" />
      <el-table-column label="表名" align="center" prop="formTableName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-refresh" @click="synchronize(scope.row)">同步</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['flowable:form:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['flowable:form:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改流程表单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="表单名称" prop="formName">
          <el-input v-model="form.formName" placeholder="请输入表单名称" />
        </el-form-item>
        <el-form-item label="表单内容">
          <editor v-model="form.formContent" :min-height="192" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--表单配置详情-->
    <el-dialog :title="formTitle" :visible.sync="formConfOpen" width="60%" append-to-body>
      <div class="test-form">
        <parser :key="new Date().getTime()" :form-conf="formConf" />
      </div>
    </el-dialog>

    <!-- 表单类型选择 -->
    <el-dialog title="表单配置" :visible.sync="isFormTypeDialog" width="30%">
      <el-form ref="form" :model="customForm" label-width="80px">
        <el-form-item label="表单类型">
          <el-select v-model="customForm.formType" placeholder="请选择表单配置">
            <el-option label="在线定制" :value="0"></el-option>
            <el-option label="自定义开发" :value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isFormTypeDialog = false">取 消</el-button>
        <el-button type="primary" @click="formTypeClick">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 自定义表单信息 -->
    <el-dialog title="自定义表单信息" :visible.sync="IsCustomForm" width="30%">
      <el-form ref="form" label-width="80px" :model="customForm">
        <el-form-item label="表单名称">
          <el-input v-model="customForm.formName" placeholder="请输入表单名称"></el-input>
        </el-form-item>
        <el-form-item label="表名">
          <el-input v-model="customForm.formTableName" placeholder="请输入表名"></el-input>
        </el-form-item>
        <el-form-item label="路由地址">
          <el-input v-model="customForm.formRouterPath" placeholder="请输入路由地址"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="customForm.remark" type="textarea" placeholder="请输入备注"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="IsCustomForm = false">取 消</el-button>
        <el-button type="primary" @click="CustomFormClick">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 路由表单弹窗 -->
    <el-dialog title="路由表单" :visible.sync="IsRouterForm" width="50%" >
      <iframe :src="routeViewPath" frameborder="0" width="100%" height="660px"></iframe>
    </el-dialog>
  </div>
</template>

<script>
import {
  listForm,
  getForm,
  delForm,
  addForm,
  updateForm,
  exportForm,
  synchronize,
} from '@/api/flowable/form'
import Editor from '@/components/Editor'
import Parser from '@/components/parser/Parser'
export default {
  name: 'Form',
  components: {
    Editor,
    Parser,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流程表单表格数据
      formList: [],
      // 弹出层标题
      title: '',
      formConf: {}, // 默认表单数据
      formConfOpen: false,
      formTitle: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        formName: null,
        formContent: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      customForm: {
        formName: '',
        formTableName: '',
        formRouterPath: '',
        remark: '',
        formType: 0,
      },
      isFormTypeDialog: false,
      IsCustomForm: false,
      isUpdate: false,
      routeViewPath: '',
      IsRouterForm: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询流程表单列表 */
    getList() {
      this.loading = true
      listForm(this.queryParams).then((response) => {
        this.formList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        formId: null,
        formName: null,
        formContent: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        remark: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.formId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 同步数据库表单 */
    synchronize(row){
      this.$confirm('确认要同步表结构吗?可能会丢失数据', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        synchronize(row).then(response => {
          return true;
        });
      }).then(() => {
        this.msgSuccess("同步成功");
      })
    },
    /** 表单配置信息 */
    handleDetail(row) {
      if (row.formType) {
        this.formConfOpen = true
        this.formTitle = '流程表单配置详细'
        this.formConf = JSON.parse(row.formContent)
      } else {
        console.log(row.formRouterPath)
        this.routeViewPath = row.formRouterPath
        this.IsRouterForm = true
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加流程表单";
      this.isFormTypeDialog = true
      this.isUpdate = false
    },
    formTypeClick() {
      this.isFormTypeDialog = false
      if (this.customForm.formType) {
        this.$router.push({
          path: '/tool/build/index',
          query: { formId: null },
        })
      } else {
        this.IsCustomForm = true
      }
    },
    CustomFormClick() {
      console.log(this.customForm)
      this.IsCustomForm = false
      if (!this.isUpdate) {
        addForm(this.customForm).then((response) => {
          this.msgSuccess('新增成功')
          this.getList()
        })
      } else {
        updateForm(this.customForm).then((response) => {
          this.msgSuccess('修改成功')
          this.getList()
        })
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      // const formId = row.formId || this.ids
      // getForm(formId).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改流程表单";
      // });
      if (row.formType) {
        this.$router.push({
          path: '/tool/build/index',
          query: { formId: row.formId },
        })
      } else {
        this.customForm = {
          ...row,
        }
        // this.customForm = {
        //   formId: row.formId,
        //   formName: row.formName,
        //   formTableName: row.formTableName,
        //   formRouterPath: row.formRouterPath,
        //   formType: row.formType,
        //   remark: row.remark,
        // }
        this.isUpdate = true
        this.IsCustomForm = true
        console.log(this.customForm)
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.formId != null) {
            updateForm(this.form).then((response) => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addForm(this.form).then((response) => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const formIds = row.formId || this.ids
      this.$confirm(
        '是否确认删除流程表单编号为"' + formIds + '"的数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      )
        .then(function () {
          return delForm(formIds)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有流程表单数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return exportForm(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
        })
    },
  },
}
</script>

<style lang="scss" scoped>
.test-form {
  margin: 15px auto;
  width: 800px;
  padding: 15px;
}
</style>
